﻿#ifndef FFWEBSOCKET_H
#define FFWEBSOCKET_H

#include <functional>
#include <iostream>
#include <list>
#include <mutex>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <string>
#include <nlohmann/json.hpp>
#include <memory>


enum WEBSOCKET_RET {
    WEBSOCKET_SUCESS = 0,
    WEBSOCKET_FAIL,
    WEBSOCKET_AGAIN
};


// 信令命名空间
namespace FF_WEBRTC_SIGNAL {
    const std::string SIGNAL_TYPE_JOIN = "join";
    const std::string SIGNAL_TYPE_RESP_JOIN = "resp_join";
    const std::string SIGNAL_TYPE_LEAVE = "leave";
    const std::string SIGNAL_TYPE_NEW_PEER = "new_peer";
    const std::string SIGNAL_TYPE_PEER_LEAVE = "peer_leave";
    const std::string SIGNAL_TYPE_OFFER = "offer";
    const std::string SIGNAL_TYPE_ANSWER = "answer";
    const std::string SIGNAL_TYPE_CANDIDATE = "candidate";
}

using json = nlohmann::json;
using websocketpp::lib::bind;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;

typedef websocketpp::client<websocketpp::config::asio_client> client;

// 抽象基类：回调函数定义为纯虚函数
class FFWebSocket {
public:
    FFWebSocket();
    virtual ~FFWebSocket() = default;

    int init();
    int connect(const std::string uri_);
    int send(const std::string msg);
    int close();
    int exec();
    int exec_one();
    int asyncExec();

protected:
    // 纯虚函数回调接口
    virtual void on_open(websocketpp::connection_hdl hdl) = 0;
    virtual void on_close(websocketpp::connection_hdl hdl) = 0;
    virtual void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) = 0;
    virtual void on_fail(websocketpp::connection_hdl hdl) = 0;


    // 保护成员：子类可访问客户端实例和连接信息
    std::string wssUri;
    websocketpp::connection_hdl wssHdl;
    std::atomic<bool>connected_ = { false };
    client wssClient;
};

#endif // FFWEBSOCKET_H
